var formFieldInfo = [];
var formId="";
//设置模版的分隔符
_.templateSettings = {
    evaluate: /\{%([\s\S]+?)%\}/g,
    interpolate: /\{\{([\s\S]+?)\}\}/g,
    escape: /\{\{-([\s\S]+?)\}\}/g
};
$(document).ready(function(){
    //國際化
    $('#formFieldSpan').text($.i18n('com.cap.ctrl.wordinjectionctrl.formfield'));
    $('#bookMarkSpan').text($.i18n('com.cap.ctrl.wordinjectionctrl.bookmark'));
    getFormFields();
});

function getFormFields(){
    var params = initParam();
    formId = params.formBaseInfo.formBaseInfo.formInfo.id;
    $.ajax({ url: "/seeyon/rest/cap4/BindUrlControl/getBindFields?formId=" + formId + "&currentFieldName="+params.currentfield.name+"&currentFormName="+params.currentfield.tableName,
        async:true,
        success: function(data){
        	var fields = data.data;
            var options = "<option></option>";
            formFieldInfo=[];
            for (var key in fields){
                var field = {};
                field.fieldName = fields[key].name;
                field.display = fields[key].display;
                field.tableName = fields[key].tableName;
                formFieldInfo = formFieldInfo.concat(field);
            }
            getwordFieldInfo();
        }
    });
}
//初始化数据
function getwordFieldInfo() {
    var params = initParam();
    var customParam=JSON.parse(params.currentfield.customParam || "{}");
    var fieldName =customParam.bindUrlField && (customParam.bindUrlField.fieldName);
    //如果之前已经设置，重新打开设置窗口的时候要显示出之前已经设置的映射        var formfield = $("#formfield");
    var formfield = $("#formfield");
    formfield.empty();
    if(params.currentfield && params.currentfield.customParam && fieldName){
        var render = _.template($("#template-word_relation").html());
        var html1 = render({ sourveValue: fieldName,targetValue:'', formFieldList:formFieldInfo});
        formfield.append($(html1.trim()));
    }else{
        var options = "<option></option>";
        for (var key in formFieldInfo){
            options += "<option fieldname="+formFieldInfo[key].fieldName+">" +"["+formFieldInfo[key].tableName+"]"+formFieldInfo[key].display + "</option>";
        }
        formfield.append(options);
    }
}


function initParam(){
    var obj= window.parentDialogObj && (window.parentDialogObj["ctrlDialog"]);//获取窗口对象
    if(obj && obj.getTransParams){
        //然后通过V5方法获取弹窗传递过来的参数
        return obj.getTransParams();
    }
}


//确定按钮调用方法，返回需要的json数据
function OK(){
    var deferred=$.Deferred();
    var settings = $("#systemMappingArea").find(".biz_groupguanlian");
    var checkTag = true;
    var temp = {};
    path="";
    var setting = $(settings[0]);
    var fieldName = setting.find(".leftField").find("option:selected").attr("fieldname");
    if(fieldName==''||fieldName==null){
    	 top.$.alert("映射参数为空，请选择");
    	 deferred.resolve({valid:false});
    	 return deferred.promise();
    }
    temp.fieldName = fieldName;
    temp.formId=formId;
    deferred.resolve({valid:true,data:temp});
    return     deferred.promise();
}

